package lingnan.cn.edu.view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.table.DefaultTableCellRenderer;

import lingnan.cn.edu.BaseAndImpl.MilkManageService;
import lingnan.cn.edu.BaseAndImpl.StaffManageService;
import lingnan.cn.edu.Table.MilkManageTableModel;
import lingnan.cn.edu.Table.OrderinfTableModel;
import lingnan.cn.edu.Table.StaffManageTableModel;
import lingnan.cn.edu.User.Milkteainf;
import lingnan.cn.edu.User.Staffinf;
import lingnan.cn.edu.chat.ChatClient;
import lingnan.cn.edu.dialog.MilkManageDialog;
import lingnan.cn.edu.dialog.StaffManageDialog;
import lingnan.cn.edu.service.MilkManageServiceImpl;
import lingnan.cn.edu.service.StaffManageServiceImpl;
import lingnan.cn.edu.util.DateUtil;
import lingnan.cn.edu.util.FontUtil;

public class ManageView extends BaseView implements ActionListener, MouseListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 3229394414733780220L;
	JMenuBar menuBar;
	JMenu settingMenu,helpMenu;
	
	JMenuItem helpMenuItem,configMenuItem;
	 
	CardLayout rightPanelLayout;
	JSplitPane containerPanel;
	JPanel leftPanel,rightPanel;
	
	JLabel logoLabel,Menu1Label,Menu2Label,Menu3Label,Menu4Label;
	
	JPanel bottomPanel;
	
	JLabel timeLabel;
	
	JFrame addmilkteaFrame,addstaffFrame;
	//
	Timer timer;
	
	private String loginName;
	//---------奶茶、员工管理------------
	private MilkManageService milkmanageService = new MilkManageServiceImpl();
	private StaffManageService staffmanageService = new StaffManageServiceImpl();
		//上部按钮
	private JPanel toolBarPanel,stafftoolBarPanel,ordertoolBarPanel;
	private JScrollPane milkscroll,staffscroll,orderscroll;
	private JPanel searchPanel,staffsearchPanel,ordersearchPanel;
	private JLabel nameLabel,staffnameLabel,ordernameLabel;
	private JTextField nameSearchTF,staffnameSearchTF,orderSearchTF;
	private JLabel typeLabel;
	private JTextField typeSearchTf;
	private JButton searchBtn,staffsearchBtn,ordersearchBtn;
	
	private JPanel opePanel,staffopePanel,orderopePanel;
	private JButton addBtn,updateBtn,deleteBtn,staffaddBtn,staffupdateBtn,staffdeleteBtn;
	
		//下部
	private JTable milkTable,staffTable,orderTable;
	
	JPanel Menu1Panel,Menu2Panel,Menu3Panel;
	//---------奶茶、员工管理结束------------
	

	
	public ManageView(String loginName) {
		super(1300,1000,"管理员界面");
		this.loginName = loginName;
		timer = new Timer(1000,this);
		timer.start();
	}

	@Override
	protected void initView() {
		
		menuBar = new JMenuBar();
		
		settingMenu = new JMenu("设置");
		
		helpMenu = new JMenu("帮助");
		
		helpMenuItem = new JMenuItem("联系客服",new ImageIcon("static\\icon\\setting.png"));
		helpMenuItem.addActionListener(this);
		
		configMenuItem = new JMenuItem("奶茶界面",new ImageIcon("static\\icon\\setting.png"));
		configMenuItem.addActionListener(this);
		
		settingMenu.add(configMenuItem);
		helpMenu.add(helpMenuItem);
		
		menuBar.add(settingMenu);
		menuBar.add(helpMenu);
		
		setJMenuBar(menuBar);
		 
		
		
		leftPanel =new JPanel();
		leftPanel.setBackground(new Color(0xADD6DC));
		leftPanel.setLayout(null);
		
		JLabel logoLabel = new JLabel(new ImageIcon("static\\img\\logo.png"));
		leftPanel.add(logoLabel);
		logoLabel.setBounds(50, 30, 200, 200);
		//奶茶管理
		Menu1Label = new JLabel("奶茶管理",new ImageIcon("static\\img\\1.png") ,JLabel.LEFT);
		Menu1Label.setBounds(60,200,300,150);
		Menu1Label.setFont(FontUtil.menuFont);
		Menu1Label.addMouseListener(this);
		
		leftPanel.add(Menu1Label);
		
		//员工管理
		Menu2Label = new JLabel("员工管理",new ImageIcon("static\\img\\2.png") ,JLabel.LEFT);
		Menu2Label.setBounds(60,300,300,150);
		Menu2Label.setFont(FontUtil.menuFont);
		Menu2Label.addMouseListener(this);
		leftPanel.add(Menu2Label);
		
		//订单管理
		Menu3Label = new JLabel("订单管理",new ImageIcon("static\\img\\3.png") ,JLabel.LEFT);
		Menu3Label.setBounds(60,400,300,150);
		Menu3Label.setFont(FontUtil.menuFont);
		Menu3Label.addMouseListener(this);
		leftPanel.add(Menu3Label);
		
		//选项卡
		rightPanelLayout = new CardLayout();
		
		//奶茶的列表
		Menu1Panel = new JPanel(new BorderLayout());
		Menu2Panel = new JPanel(new BorderLayout());
		Menu3Panel = new JPanel(new BorderLayout());
		
		//--------奶茶管理上部按钮添加----------
		toolBarPanel = new JPanel(new BorderLayout()); 
		toolBarPanel.setBackground(Color.white);
		searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 
		searchPanel.setBackground(Color.white);
		nameLabel = new JLabel("奶茶名称");
		nameSearchTF = new JTextField(10);
		nameLabel.setBackground(Color.white);
		typeLabel = new JLabel("类型");
		typeSearchTf = new JTextField(10);
		typeLabel.setBackground(Color.white);
		
		searchBtn = new JButton("搜索",new ImageIcon("static\\icon\\milk_search.png"));
		searchBtn.addActionListener(this);
		searchBtn.setBackground(new Color(0xADD6DC));
		opePanel =  new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
		opePanel.setBackground(Color.white);
		addBtn =new JButton("添加",new ImageIcon("static\\icon\\milk_add.png"));
		addBtn.setBackground(new Color(0xADD6DC));
		updateBtn =new JButton("更新",new ImageIcon("static\\icon\\milk_edit.png"));
		updateBtn.setBackground(new Color(0xADD6DC));
		deleteBtn =new JButton("删除",new ImageIcon("static\\icon\\milk_delete.png"));
		deleteBtn.setBackground(new Color(0xADD6DC));
		
		addBtn.addActionListener(this);
		updateBtn.addActionListener(this);
		deleteBtn.addActionListener(this);
		
		opePanel.add(addBtn);
		opePanel.add(updateBtn);
		opePanel.add(deleteBtn);
		 
		searchPanel.add(nameLabel);
		searchPanel.add(nameSearchTF);
		searchPanel.add(typeLabel);
		searchPanel.add(typeSearchTf);
		searchPanel.add(searchBtn);
		
		
		toolBarPanel.add(searchPanel,"West");
		toolBarPanel.add(opePanel,"East");
		
		//-----奶茶上部按钮添加结束---------
		
		//奶茶下部表格添加
		MilkManageTableModel milkTableModel = new MilkManageTableModel();
		
		milkTableModel.query();
		milkTable = new JTable(milkTableModel);
		milkTable.setFont(FontUtil.menuFont2);
		milkTable.setShowGrid(false);
		milkscroll = new JScrollPane(milkTable);
		//居中
				DefaultTableCellRenderer r=new DefaultTableCellRenderer();
		        r.setHorizontalAlignment(JLabel.CENTER);
		        milkTable.setRowHeight(30);
		        milkTable.setDefaultRenderer(Object.class,r);
		//员工下部表格添加
		StaffManageTableModel staffTableModel = new StaffManageTableModel();
		staffTableModel.query();
		staffTable = new JTable(staffTableModel);
		staffTable.setFont(FontUtil.menuFont2);
		staffTable.setShowGrid(false);
		staffscroll = new JScrollPane(staffTable);
		//
		staffTable.setRowHeight(30);
		staffTable.setDefaultRenderer(Object.class,r);
		//订单下部表格添加
		OrderinfTableModel orderinfTableModel = new OrderinfTableModel();
		orderinfTableModel.query();
		orderTable = new JTable(orderinfTableModel);
		orderTable.setFont(FontUtil.menuFont2);
		orderTable.setShowGrid(false);
		orderscroll = new JScrollPane(orderTable);
		//
		orderTable.setRowHeight(30);
		orderTable.setDefaultRenderer(Object.class,r);
		//奶茶管理面板添加
		Menu1Panel.add(milkscroll,"Center");
		Menu1Panel.add(toolBarPanel,"North");	
		
		//员工管理上部按钮添加//
		stafftoolBarPanel = new JPanel(new BorderLayout()); 
		stafftoolBarPanel.setBackground(Color.white);
		staffsearchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 
		staffsearchPanel.setBackground(Color.white);
		staffnameLabel = new JLabel("员工姓名");
		staffnameSearchTF = new JTextField(10);
				 
		
		staffsearchBtn = new JButton("搜索",new ImageIcon("static\\icon\\milk_search.png"));
		staffsearchBtn.addActionListener(this);
		staffsearchBtn.setBackground(new Color(0xADD6DC));
		staffopePanel =  new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
		staffopePanel.setBackground(Color.white);
		staffaddBtn =new JButton("添加",new ImageIcon("static\\icon\\milk_add.png"));
		staffaddBtn.setBackground(new Color(0xADD6DC));
		staffupdateBtn =new JButton("更新",new ImageIcon("static\\icon\\milk_edit.png"));
		staffupdateBtn.setBackground(new Color(0xADD6DC));
		staffdeleteBtn =new JButton("删除",new ImageIcon("static\\icon\\milk_delete.png"));
		staffdeleteBtn.setBackground(new Color(0xADD6DC));
		
		staffaddBtn.addActionListener(this);
		staffupdateBtn.addActionListener(this);
		staffdeleteBtn.addActionListener(this);
		
		staffopePanel.add(staffaddBtn);
		staffopePanel.add(staffupdateBtn);
		staffopePanel.add(staffdeleteBtn);
		 
		staffsearchPanel.add(staffnameLabel);
		staffsearchPanel.add(staffnameSearchTF);
		staffsearchPanel.add(staffsearchBtn);
		
		
		stafftoolBarPanel.add(staffsearchPanel,"West");
		stafftoolBarPanel.add(staffopePanel,"East");
		
		//添加结束//
		
		//订单按钮添加
		ordertoolBarPanel = new JPanel(new BorderLayout()); 
		ordertoolBarPanel.setBackground(Color.white);
		ordersearchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 
		ordernameLabel = new JLabel("订单编号");
		ordersearchPanel.setBackground(Color.white);
		orderSearchTF = new JTextField(10);
				 
		
		ordersearchBtn = new JButton("搜索",new ImageIcon("static\\icon\\milk_search.png"));
		ordersearchBtn.addActionListener(this);
		ordersearchBtn.setBackground(new Color(0xADD6DC));
		orderopePanel =  new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
		
		 
		ordersearchPanel.add(ordernameLabel);
		ordersearchPanel.add(orderSearchTF);
		ordersearchPanel.add(ordersearchBtn);
		
		
		ordertoolBarPanel.add(ordersearchPanel,"West");
		ordertoolBarPanel.add(orderopePanel,"East");
		
		Menu3Panel.add(ordertoolBarPanel,"North");
		Menu3Panel.add(orderscroll,"Center");
		//添加结束
		
		
		//按钮添加
		Menu2Panel.add(stafftoolBarPanel,"North");
		Menu2Panel.add(staffscroll,"Center");
		//
		rightPanel = new JPanel(rightPanelLayout);
		rightPanel.add(Menu1Panel, "0");
		rightPanel.add(Menu2Panel, "1");
		rightPanel.add(Menu3Panel, "2");
		rightPanel.setFont(FontUtil.menuFont3);
		containerPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftPanel,rightPanel);
		containerPanel.setDividerLocation(300);
		containerPanel.setDividerSize(0);
		
		bottomPanel = new JPanel();//默认的布局是流式布局
		 
		bottomPanel.setBackground(new Color(0x59C593));
		bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		
		timeLabel = new JLabel(DateUtil.dateToString(new Date(),null));
		
		bottomPanel.add(timeLabel);
		Container container = getContentPane();
		container.add(containerPanel,"Center");
		container.add(bottomPanel,"South");
		
		//refresh();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		timeLabel.setText(DateUtil.dateToString(new Date(), null));
		Object source = e.getSource();
		if(source==configMenuItem) {
			new MainView(loginName);
			this.dispose();
		}
		
		//查找奶茶功能
		if (source == searchBtn) {
			
			if ("".equals(nameSearchTF.getText().trim()) && "".equals(typeSearchTf.getText().trim())) {
				//奶茶名称和类型都为空，查找全部
				//JOptionPane.showMessageDialog(this, "请输入搜索内容");
				refresh();
			} else if (!"".equals(nameSearchTF.getText().trim()) && "".equals(typeSearchTf.getText().trim())) {
				//类型为空，按奶茶名称查找
				MilkManageTableModel findMilkByNameModel = new MilkManageTableModel();
				findMilkByNameModel.findMilkTea(nameSearchTF.getText(),null);
				milkTable.setModel(findMilkByNameModel);			
			} else if ("".equals(nameSearchTF.getText().trim()) && !"".equals(typeSearchTf.getText().trim())) {
				//奶茶名称为空，按类型查找
				MilkManageTableModel findMilkByTypeModel = new MilkManageTableModel();
				findMilkByTypeModel.findMilkTea(null,typeSearchTf.getText());
				milkTable.setModel(findMilkByTypeModel);
			}else {
				MilkManageTableModel findMilkModel = new MilkManageTableModel();
				findMilkModel.findMilkTea(nameSearchTF.getText(),typeSearchTf.getText());
				milkTable.setModel(findMilkModel);
			}
			
		}
		
		//添加奶茶功能
		if(source==addBtn) {
			MilkManageDialog milkmanageDialog = new MilkManageDialog(addmilkteaFrame,this,"添加");
			milkmanageDialog.setVisible(true);
			refresh();
		}
		//删除
		if(source==deleteBtn) {
			int rowIndex = milkTable.getSelectedRow();
			if(rowIndex==-1) {
				JOptionPane.showMessageDialog(this,"请选中一条");
				return;
			}
			String mteaName = (String)milkTable.getValueAt(rowIndex,1);
			if(milkmanageService.deleteByName(mteaName)==1) {
				JOptionPane.showMessageDialog(this,"删除成功","提示",JOptionPane.INFORMATION_MESSAGE);
			    refresh();
			}else {
				JOptionPane.showMessageDialog(this,"删除失败","提示",JOptionPane.ERROR_MESSAGE);
			}
		}
		
		if(source==updateBtn) {
			int rowIndex = milkTable.getSelectedRow();
			if(rowIndex==-1) {
				JOptionPane.showMessageDialog(this,"请选中一条");
				return;
			}
			String mteaName = (String)milkTable.getValueAt(rowIndex,1);
			int id = (Integer)milkTable.getValueAt(rowIndex,0);
			int price = (Integer)milkTable.getValueAt(rowIndex,2);
			String img = (String)milkTable.getValueAt(rowIndex,3);
			String type = (String)milkTable.getValueAt(rowIndex,4);
			if(milkmanageService.findMilkTea(mteaName, null)==null) {
				JOptionPane.showMessageDialog(this,"数据不存在,请刷新");
				return;
			}
			Milkteainf milkteainf = new Milkteainf();
			milkteainf.setName(mteaName);
			milkteainf.setId(id);
			milkteainf.setPrice(price);
			milkteainf.setImg(img);
			milkteainf.setType(type);
			MilkManageDialog milkmanageDialog = new MilkManageDialog(addmilkteaFrame,this,"更新",milkteainf);
			milkmanageDialog.setVisible(true);
		}
		if (source == staffsearchBtn) {
			if ("".equals(staffnameSearchTF.getText().trim())) {
				//员工为空，查找全部
				//JOptionPane.showMessageDialog(this, "请输入搜索内容");
				staffrefresh();
				
			}else {
				StaffManageTableModel findStaffByNameModel = new StaffManageTableModel();
				findStaffByNameModel.findStaff(staffnameSearchTF.getText());
				staffTable.setModel(findStaffByNameModel);
			}
		}
		
		//添加员工功能
				if(source==staffaddBtn) {
					StaffManageDialog staffmanageDialog = new StaffManageDialog(addstaffFrame,this,"添加");
					staffmanageDialog.setVisible(true);
					refresh();
				}
				//删除
				if(source==staffdeleteBtn) {
					int rowIndex = staffTable.getSelectedRow();
					if(rowIndex==-1) {
						JOptionPane.showMessageDialog(this,"请选中一条");
						return;
					}
					String staffName = (String)staffTable.getValueAt(rowIndex,0);
					if(staffmanageService.deleteByName(staffName)==1) {
						JOptionPane.showMessageDialog(this,"删除成功","提示",JOptionPane.INFORMATION_MESSAGE);
					    staffrefresh();
					}else {
						JOptionPane.showMessageDialog(this,"删除失败","提示",JOptionPane.ERROR_MESSAGE);
					}
				}
				
				if(source==staffupdateBtn) {
					int rowIndex = staffTable.getSelectedRow();
					if(rowIndex==-1) {
						JOptionPane.showMessageDialog(this,"请选中一条");
						return;
					}
					String staffName = (String)staffTable.getValueAt(rowIndex,0);
					String password = (String)staffTable.getValueAt(rowIndex,1);
					int staffSuper = (Integer)staffTable.getValueAt(rowIndex,2);				
					if(staffmanageService.findStaff(staffName)==null) {
						JOptionPane.showMessageDialog(this,"数据不存在,请刷新");
						return;
					}
					Staffinf staffinf = new Staffinf();
					staffinf.setStaffName(staffName);
					staffinf.setPassword(password);
					staffinf.setStaffSuper(staffSuper);
					StaffManageDialog staffmanageDialog = new StaffManageDialog(addstaffFrame,this,"更新",staffinf);
					staffmanageDialog.setVisible(true);
				}
				
				if(source==ordersearchBtn) {
					if(!"".equals(orderSearchTF.getText().trim())) {
						OrderinfTableModel findOrderModel = new OrderinfTableModel();
						findOrderModel.findOrder(Integer.parseInt(orderSearchTF.getText()));
						orderTable.setModel(findOrderModel);
					}else {
						orderRefresh();
					}
					
				}
			if (source == helpMenuItem) {
					// TODO 联系管理员
				try {
						for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
							if ("Windows".equals(info.getName())) {
								UIManager.setLookAndFeel(info.getClassName());
								break;
							}
						}
					} catch (Exception exception) {
						exception.printStackTrace();
					}
					ChatClient client = new ChatClient();
					new Thread(client).start();
			}
	}

	

	@Override
	public void mouseClicked(MouseEvent e) {
		Object source = e.getSource();
		if(source==Menu1Label) {
			rightPanelLayout.show(rightPanel,"0");
		}
		if(source==Menu2Label) {
			rightPanelLayout.show(rightPanel,"1");
		}
		if(source==Menu3Label) {
			rightPanelLayout.show(rightPanel,"2");
		}
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		Object source = e.getSource();
		if(source==Menu1Label) {
			Menu1Label.setForeground(Color.white);
		}
		else if(source==Menu2Label) {
			Menu2Label.setForeground(Color.white);
		}
		else if(source==Menu3Label) {
			Menu3Label.setForeground(Color.white);
		}
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		Object source = e.getSource();
		if(source==Menu1Label) {
			Menu1Label.setForeground(Color.black);
		}
		else if(source==Menu2Label) {
			Menu2Label.setForeground(Color.black);
		}
		else if(source==Menu3Label) {
			Menu3Label.setForeground(Color.black);
		}

	}
	
	/*public static void main(String[] args) {
		ManageView manage = new ManageView();
	}*/
	
	//刷新
	public void refresh() {
				
		MilkManageTableModel findAllMilkModel = new MilkManageTableModel();
		findAllMilkModel.query();							
		milkTable.setModel(findAllMilkModel);	
		milkTable.setBackground(Color.white);
	}
	
	public void staffrefresh() {				
		StaffManageTableModel findAllStaffModel = new StaffManageTableModel();
		findAllStaffModel.query();					
		staffTable.setModel(findAllStaffModel);
		staffTable.setBackground(Color.white);
	}
	
	public void orderRefresh() {
		OrderinfTableModel findAllOrderinfModel = new OrderinfTableModel();
		findAllOrderinfModel.query();
		orderTable.setModel(findAllOrderinfModel);
		orderTable.setBackground(Color.white);
	}
}
